﻿using MS.Common.Infr.In;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MS.Common.Infr.Im
{
    public class BaseRepository<T>: IBaseRepository<T> where T : class
    {
        private readonly DataDbContext db;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="db"> 数据库上下文 </param>
        public BaseRepository(DataDbContext db)
        {
            this.db = db;
        }

        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <returns> 所有数据</returns>
        public IQueryable<T> GetAll()
        {
            return db.Set<T>().AsQueryable();
        }
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="entity"> 要添加的数据</param>
        /// <returns> 返回受影响行数</returns>
        public int Add(T entity)
        {
            db.Set<T>().Add(entity);
            return db.SaveChanges();
        }
        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="entity"> 要更新的数据</param>
        /// <returns> 返回受影响行数</returns>
         public int Update(T entity)
        {
            db.Set<T>().Update(entity);
            return db.SaveChanges();
        }
    }
}
